home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 January / macpower199701.bin / AMUG / Programming_10 / WASTE 1.3a1.sit / WASTE 1.3a1 Distribution / WASTE 1.3a1 / WEAccessors.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-23  |  6.2 KB  |  322 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WEAccessors.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Accessors
  6.  *
  7.  *  Copyright (c) 1993-1996 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. pascal SInt32 WEOffsetToLine (SInt32 offset, WEHandle hWE)
  18. {
  19.     // given a byte offset into the text, find the corresponding line index
  20.  
  21.     WEPtr pWE = *hWE;
  22.     LineArrayPtr pLines = *pWE->hLines;
  23.     SInt32 minIndex, maxIndex, index;
  24.  
  25.     // do a fast binary search through the line array
  26.     minIndex = 0;
  27.     maxIndex = pWE->nLines;
  28.  
  29.     while (minIndex < maxIndex)
  30.     {
  31.         index = (minIndex + maxIndex) >> 1;
  32.         if (offset >= pLines[index].lineStart)
  33.         {
  34.             if (offset < pLines[index + 1].lineStart)
  35.             {
  36.                 break;
  37.             }
  38.             else
  39.             {
  40.                 minIndex = index + 1;
  41.             }
  42.         }
  43.         else
  44.         {
  45.             maxIndex = index;
  46.         }
  47.     }
  48.  
  49.     return index;
  50. }
  51.  
  52. pascal SInt32 WECountLines(WEHandle hWE)
  53. {
  54.     return (*hWE)->nLines;
  55. }
  56.  
  57. pascal void WEGetLineRange(SInt32 lineIndex, SInt32 *lineStart, SInt32 *lineEnd, WEHandle hWE)
  58. {
  59.     WEPtr pWE = *hWE;
  60.     LineRec *pLine;
  61.  
  62.     pLine = *pWE->hLines + _WEPinInRange(lineIndex, 0, pWE->nLines - 1);
  63.     if (lineStart != nil)
  64.     {
  65.         *lineStart = pLine[0].lineStart;
  66.     }
  67.     if (lineEnd != nil)
  68.     {
  69.         *lineEnd = pLine[1].lineStart;
  70.     }
  71. }
  72.  
  73. pascal SInt32 WEOffsetToRun (SInt32 offset, WEHandle hWE)
  74. {
  75.     // given a byte offset into the text, find the corresponding style run index
  76.  
  77.     WEPtr pWE = *hWE;
  78.     RunArrayPtr pRuns = *pWE->hRuns;
  79.     SInt32 minIndex, maxIndex, index;
  80.  
  81.     // do a fast binary search through the style run array
  82.     minIndex = 0;
  83.     maxIndex = pWE->nRuns;
  84.  
  85.     while (minIndex < maxIndex)
  86.     {
  87.         index = (minIndex + maxIndex) >> 1;
  88.         if (offset >= pRuns[index].runStart)
  89.         {
  90.             if (offset < pRuns[index + 1].runStart)
  91.             {
  92.                 break;
  93.             }
  94.             else
  95.             {
  96.                 minIndex = index + 1;
  97.             }
  98.         }
  99.         else
  100.         {
  101.             maxIndex = index;
  102.         }
  103.     }
  104.  
  105.     return index;
  106. }
  107.  
  108. pascal SInt32 WECountRuns(WEHandle hWE)
  109. {
  110.     return (*hWE)->nRuns;
  111. }
  112.  
  113. pascal void WEGetRunRange(SInt32 runIndex, SInt32 *runStart, SInt32 *runEnd, WEHandle hWE)
  114. {
  115.     WEPtr pWE = *hWE;
  116.     RunArrayElement *pRun;
  117.  
  118.     pRun = *pWE->hRuns + _WEPinInRange(runIndex, 0, pWE->nRuns - 1);
  119.     if (runStart != nil)
  120.     {
  121.         *runStart = pRun[0].runStart;
  122.     }
  123.     if (runEnd != nil)
  124.     {
  125.         *runEnd = pRun[1].runStart;
  126.         if (*runEnd > pWE->textLength)
  127.         {
  128.             *runEnd = pWE->textLength;
  129.         }
  130.     }
  131. }
  132.  
  133. pascal SInt32 _WEPixelToLine(SInt32 vOffset, WEHandle hWE)
  134. {
  135.     // given a vertical pixel offset in local coordinates,
  136.     // find the corresponding line index
  137.  
  138.     WEPtr pWE = *hWE;
  139.     LineArrayPtr pLines = *pWE->hLines;
  140.     SInt32 minIndex, maxIndex, index;
  141.  
  142.     // do a fast binary search through the line array
  143.     minIndex = 0;
  144.     maxIndex = pWE->nLines;
  145.  
  146.     while (minIndex < maxIndex)
  147.     {
  148.         index = (minIndex + maxIndex) >> 1;
  149.         if (vOffset >= pLines[index].lineOrigin)
  150.         {
  151.             if (vOffset < pLines[index + 1].lineOrigin)
  152.             {
  153.                 break;
  154.             }
  155.             else
  156.             {
  157.                 minIndex = index + 1;
  158.             }
  159.         }
  160.         else
  161.         {
  162.             maxIndex = index;
  163.         }
  164.     }
  165.  
  166.     return index;
  167. }
  168.  
  169. pascal void _WEGetIndStyle(SInt32 runIndex, WERunInfo *info, WEHandle hWE)
  170. {
  171.     WEPtr pWE = *hWE;    // assume WE record is already locked
  172.     RunArrayElementPtr pRun;
  173.  
  174.     // get a pointer to the specified run array element
  175.     pRun = *pWE->hRuns + runIndex;
  176.  
  177.     // fill in the runStart and runEnd fields from the style run array
  178.     info->runStart = pRun[0].runStart;
  179.     info->runEnd = pRun[1].runStart;
  180.  
  181.     // copy the style information from the appropriate entry in the style table
  182.     info->runAttrs = (*pWE->hStyles)[pRun->styleIndex].info;
  183. }
  184.  
  185. pascal Boolean _WEGetIndDirection(SInt32 runIndex, WEHandle hWE)
  186. {
  187.     WEPtr pWE = *hWE;
  188.  
  189.     // the high bit of tsFlags is set if the corresponding run is right-to-left
  190.     return BTST((*pWE->hStyles + (*pWE->hRuns + runIndex)->styleIndex)->info.runStyle.tsFlags, tsRightToLeft) ? true : false;
  191. }
  192.  
  193. pascal Boolean WEGetRunDirection(SInt32 offset, WEHandle hWE)
  194. {
  195.     WEPtr pWE = *hWE;
  196.  
  197.     //    if offset is out of range, return the primary line direction
  198.     if ((offset < 0) || (offset >= pWE->textLength))
  199.     {
  200.         return IsRightToLeft(pWE->direction);
  201.     }
  202.  
  203.     return _WEGetIndDirection(WEOffsetToRun(offset, hWE), hWE);
  204. }
  205.  
  206. pascal void WEGetRunInfo(SInt32 offset, WERunInfo *info, WEHandle hWE)
  207. {
  208.     _WEGetIndStyle(WEOffsetToRun(offset, hWE), info, hWE);
  209. }
  210.  
  211. pascal SInt32 WEGetHeight(SInt32 startLine, SInt32 endLine, WEHandle hWE)
  212. {
  213.     WEPtr pWE = *hWE;
  214.     LineArrayPtr pLines = *pWE->hLines;
  215.     SInt32 nLines = pWE->nLines;
  216.  
  217.     startLine = _WEPinInRange(startLine, 0, nLines);
  218.     endLine = _WEPinInRange(endLine, 0, nLines);
  219.     _WEReorder(&startLine, &endLine);
  220.     return pLines[endLine].lineOrigin - pLines[startLine].lineOrigin;
  221. }
  222.  
  223. pascal Handle WEGetText(WEHandle hWE)
  224. {
  225.     return (*hWE)->hText;
  226. }
  227.  
  228. pascal SInt16 WEGetChar(SInt32 offset, WEHandle hWE)
  229. {
  230.     WEPtr pWE = *hWE;
  231.  
  232.     // sanity check: make sure offset is withing allowed bounds
  233.     if ((offset < 0) || (offset >= pWE->textLength))
  234.     {
  235.         return 0;
  236.     }
  237.  
  238.     // get the specified character (actually, byte)
  239.     return * (UInt8 *) (*pWE->hText + offset);
  240. }
  241.  
  242. pascal WEAlignment WEGetAlignment(WEHandle hWE)
  243. {
  244.     return (*hWE)->alignment;
  245. }
  246.  
  247. pascal WEDirection WEGetDirection(WEHandle hWE)
  248. {
  249.     return (*hWE)->direction;
  250. }
  251.  
  252. pascal void WEGetSelection(SInt32 *selStart, SInt32 *selEnd, WEHandle hWE)
  253. {
  254.     WEPtr pWE = *hWE;
  255.  
  256.     *selStart = pWE->selStart;
  257.     *selEnd = pWE->selEnd;
  258. }
  259.  
  260. pascal void WESetDestRect(const LongRect *destRect, WEHandle hWE)
  261. {
  262.     (*hWE)->destRect = *destRect;
  263. }
  264.  
  265. pascal void WEGetDestRect(LongRect *destRect, WEHandle hWE)
  266. {
  267.     *destRect = (*hWE)->destRect;
  268. }
  269.  
  270. pascal void WESetViewRect(const LongRect *viewRect, WEHandle hWE)
  271. {
  272.     WEPtr pWE = *hWE;
  273.     Rect r;
  274.  
  275.     pWE->viewRect = *viewRect;
  276.  
  277.     // keep the viewRgn in sync with the view rectangle
  278.     WELongRectToRect(viewRect, &r);
  279.     RectRgn(pWE->viewRgn, &r);
  280. }
  281.  
  282. pascal void WEGetViewRect(LongRect *viewRect, WEHandle hWE)
  283. {
  284.     *viewRect = (*hWE)->viewRect;
  285. }
  286.  
  287. pascal SInt32 WEGetTextLength(WEHandle hWE)
  288. {
  289.     return (*hWE)->textLength;
  290. }
  291.  
  292. pascal SInt16 WEFeatureFlag(SInt16 feature, SInt16 action, WEHandle hWE)
  293. {
  294.     WEPtr pWE = *hWE;
  295.     SInt16 status;
  296.  
  297.     // get current status of the specified feature
  298.     status = BTST(pWE->features, feature) ? weBitSet : weBitClear;
  299.  
  300.     // if action is weBitToggle, invert flag
  301.     if (action == weBitToggle)
  302.         action = 1 - status;
  303.  
  304.     // reset flag according to action
  305.     if (action == weBitClear)
  306.     {
  307.         BCLR(pWE->features, feature);
  308.     }
  309.     else if (action == weBitSet)
  310.     {
  311.         BSET(pWE->features, feature);
  312.     }
  313.  
  314.     // return old status
  315.     return status;
  316. }
  317.  
  318. pascal UInt32 WEVersion(void)
  319. {
  320.     return WASTE_VERSION ;
  321. }
  322.